import Application from "../application";
import * as log4js from 'koa-log4';
import {join} from 'path';

const logger = log4js.getLogger('application');


const logTask = () => {
    const app = Application.Instance.getApp();

    log4js.configure({
        appenders: {
            access: {
                type: 'dateFile',
                pattern: '-yyyy-MM-dd.log',
                filename: join('logs/', 'access.log')
            },
            application: {
                type: 'dateFile',
                pattern: '-yyyy-MM-dd.log',
                filename: join('logs/', 'application.log')
            },
            out: {
                type: 'console'
            }
        },
        categories: {
            default: {appenders: ['out'], level: 'info'},
            access: {appenders: ['access'], level: 'info'},
            application: {appenders: ['application'], level: 'WARN'}
        }
    });

    app.use(log4js.koaLogger(log4js.getLogger('access')));


    app.on('error', err => {
        logger.error(err);
    });
};

export {
    logTask,
    logger
};
